home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / AGNUS / INC++ / SETOFCHR.H
Encoding:
C/C++ Source or Header  |  1996-05-14  |  5.0 KB  |  220 lines

  1. /*
  2.  *    eXtended-Library
  3.  *    released for GNU-C++
  4.  *    by Volker Hemsen, 05/96
  5.  *    Public Domain / Freeware
  6.  *
  7.  *    Pascal-like 'set of char'
  8.  *    but uses 256 chars !!!
  9.  *    Operators: = == != + += - -= & &&(=Pascal-in)
  10.  *    Operands: SET_OF_CHAR char char*
  11.  */
  12.  
  13. #ifdef __cplusplus
  14. #ifndef _SETOFCHAR_H
  15. #define _SETOFCHAR_H
  16.  
  17. #include <bool.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20.  
  21. class SET_OF_CHAR
  22. {
  23. private:
  24.     long bits[8];                /* Bitvektor */
  25.     void clr(char c)        /* Bit c löschen    */
  26.     {
  27.         bits[c/32]&=~(1L<<(c%32));
  28.     }
  29.     void clrs(char *cs)    /* Bits cs[0].. löschen    */
  30.     {
  31.         while (*cs)
  32.             clr(*cs++);
  33.     }
  34.     void clrall(void)        /* alle Bits loeschen */
  35.     {
  36.         register int ii;
  37.         for(ii=0;ii<=7;bits[ii++]=0L);
  38.     }
  39.     void set(char c)        /* Bit c setzen    */
  40.     {
  41.         bits[c/32]|=(1L<<(c%32));
  42.     }
  43.     void sets(char *cs)    /* Bits cs[0].. löschen */
  44.     {
  45.         while (*cs)
  46.             set(*cs++);
  47.     }
  48.     bool get(char c)            /* Bit c prüfen    */
  49.     {
  50.         return (bits[c/32]&(1L<<(c%32)));
  51.     }
  52.     void clrbits(long *l)    /* alle in l gesetzen Bits im Vektor löschen    */
  53.     {
  54.         register int ii;
  55.         for(ii=0;ii<=7;ii++)
  56.             bits[ii]&=~(*l++);
  57.     }
  58.     void setbits(long *l)    /* alle in l gesetzen Bits im Vektor setzen    */
  59.     {
  60.         register int ii;
  61.         for(ii=0;ii<=7;ii++)
  62.             bits[ii]|=(*l++);
  63.     }
  64.     bool cmpbits(long *l)    /* Inhalt von Vektor l mit bits vergleichen    */
  65.     {
  66.         register int ii;
  67.         for(ii=0;ii<=7;ii++)
  68.             if (bits[ii]!=*l++)
  69.                 return FALSE;
  70.         return TRUE;
  71.     }
  72.     void andbits(long *l)    /*    Inhalt des Bitvektors mit l undieren    */
  73.     {
  74.         register int ii;
  75.         for(ii=0;ii<=7;ii++)
  76.             bits[ii]&=*l++;
  77.     }
  78.     void cpyfrombits(long *to)    /* Inhalt von bits nach to kopieren    */
  79.     {
  80.         memcpy(to,bits,32);
  81.     }
  82.     void cpytobits(long *from)    /* Inhalt von from nach bits kopieren    */
  83.     {
  84.         memcpy(bits,from,32);
  85.     }
  86. public:
  87. /*    Konstruktoren    */
  88.     inline SET_OF_CHAR() {    clrall();    };
  89.     inline SET_OF_CHAR(SET_OF_CHAR& s) {    cpytobits(s.bits);    }
  90.     SET_OF_CHAR(char c) {    clrall();    set(c);    }
  91.     SET_OF_CHAR(char *cs) {    clrall();    sets(cs);    }
  92. /*    Operator = : Zuweisungen    */
  93.     void operator = (char c)    { clrall();    set(c);    }
  94.     inline void operator = (SET_OF_CHAR s) {    cpytobits(s.bits);    }
  95.     void operator = (char *cs) {    clrall();    sets(cs);    }
  96. /*    Operator + : Verknüpfungen    */
  97.     SET_OF_CHAR operator + (SET_OF_CHAR a) {
  98.         SET_OF_CHAR s=a;
  99.         register int ii;
  100.         for(ii=0;ii<=7;s.bits[ii]|=bits[ii],ii++);
  101.         return s;
  102.     }
  103.     SET_OF_CHAR operator + (char c) {
  104.         SET_OF_CHAR s;
  105.         cpyfrombits(s.bits);
  106.         s.set(c);
  107.         return s;
  108.     }
  109.     SET_OF_CHAR operator + (char *cs) {
  110.         SET_OF_CHAR s;
  111.         cpyfrombits(s.bits);
  112.         s.sets(cs);
  113.         return s;
  114.     }
  115. /*    Operator += : Verknüpfen und Zuweisen    */
  116.     inline void operator += (char c) { set(c);    }
  117.     inline void operator += (char *cs) { sets(cs);    }
  118.     inline void operator += (SET_OF_CHAR a) {    setbits(a.bits);    }
  119. /*    Operator - : Ausgrenzen    */
  120.     SET_OF_CHAR operator - (char c) {
  121.         SET_OF_CHAR s;
  122.         cpyfrombits(s.bits);
  123.         s.clr(c);
  124.         return s;
  125.     }
  126.     SET_OF_CHAR operator - (char *cs) {
  127.         SET_OF_CHAR s;
  128.         cpyfrombits(s.bits);
  129.         s.clrs(cs);
  130.         return s;
  131.     }
  132.     SET_OF_CHAR operator - (SET_OF_CHAR a) {
  133.         SET_OF_CHAR s;
  134.         cpyfrombits(s.bits);
  135.         s.clrbits(a.bits);
  136.         return s;
  137.     }
  138. /*    Operator -= : Ausgrenzen und Zuweisen    */
  139.     inline void operator -= (char c) { clr(c);    }
  140.     inline void operator -= (char *cs) { clrs(cs);    }
  141.     inline void operator -= (SET_OF_CHAR a) {    clrbits(a.bits);    }
  142. /*    Operator & : Schnittmenge bilden    */
  143.     SET_OF_CHAR operator & (char c) {
  144.         SET_OF_CHAR s=c;
  145.         s.andbits(bits);
  146.         return s;
  147.     }
  148.     SET_OF_CHAR operator & (char *cs) {
  149.         SET_OF_CHAR s=cs;
  150.         s.andbits(bits);
  151.         return s;
  152.     }
  153.     SET_OF_CHAR operator & (SET_OF_CHAR a) {
  154.         SET_OF_CHAR s=a;
  155.         s.andbits(bits);
  156.         return s;
  157.     }
  158. /*    Operator && : auf Bestandteil prüfen    */
  159.     inline bool operator && (char c) {    return (get(c)!=0); }
  160.     bool operator && (char *cs) {
  161.         while (*cs)
  162.             if (!get(*cs++))
  163.                 return FALSE;
  164.         return TRUE;
  165.     }
  166.     bool operator && (SET_OF_CHAR a) {
  167.         register int ii;
  168.         for(ii=0;ii<=255;ii++) {
  169.             if ((a.get((char)ii)) && (!get((char)ii)))
  170.                 return FALSE;
  171.         }
  172.         return TRUE;
  173.     }
  174. /*    Operator == : kompletten Inhalt vergleichen    */
  175.     bool operator == (char c) {
  176.         SET_OF_CHAR s=c;
  177.         return cmpbits(s.bits);
  178.     }
  179.     bool operator == (char *cs) {
  180.         SET_OF_CHAR s=cs;
  181.         return cmpbits(s.bits);
  182.     }
  183.     inline bool operator == (SET_OF_CHAR a) {    return cmpbits(a.bits);    }
  184. /*    Operator != : kompletten Inhalt vergleichen, negative Reaktion    */
  185.     bool operator != (char c) {
  186.         SET_OF_CHAR s=c;
  187.         return !cmpbits(s.bits);
  188.     }
  189.     bool operator != (char *cs) {
  190.         SET_OF_CHAR s=cs;
  191.         return !cmpbits(s.bits);
  192.     }
  193.     inline bool operator != (SET_OF_CHAR a) {    return !cmpbits(a.bits);    }
  194. /*    internen Bitvektor zurückgeben    */
  195.     inline long *bitptr(void) {    return bits;    }
  196. /*    set in einen String zurückwandeln    */
  197.     char *out(char *cs) {
  198.         register char *cp=cs;
  199.         register ii;
  200.         for(ii=1;ii<=255;ii++) {
  201.             if (get((char)ii))
  202.                 *cp++=(char)ii;
  203.         }
  204.         *cp='\0';
  205.         return cs;
  206.     }
  207. /*    set in einen String wandeln und auf Kanal std ausgeben, next anhängen    */
  208.     void cout(FILE *std,char *next) {
  209.         char s[256];
  210.         if (std==NULL)
  211.             std=stdout;
  212.         fprintf(std,"%s",out(s));
  213.         if ((next!=NULL) && (*next!=0))
  214.             fprintf(std,"%s",next);
  215.     }
  216. };
  217.  
  218. #endif    /*    #ifndef _SETOFCHAR_H    */
  219. #endif    /*    #ifdef __cplusplus    */
  220.